﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WFcontrol.BL
{
    class cl_Danhmuc
    {
        accessdata con;
        Form frm;
        DataTable tb;
        //string SQL;
        public cl_Danhmuc(Frm_danhmuc f)
        {
            con = new accessdata();
            frm = f;
        }
        public void refreshdata()
        {
            ((DataGridView)frm.Controls["dataGridview1"]).DataSource = tb;
             ((TextBox)frm.Controls["textBox1"]).DataBindings.Clear();
             ((TextBox)frm.Controls["textBox2"]).DataBindings.Clear();

            ((TextBox)frm.Controls["textBox1"]).DataBindings.Add("text", tb, "MaDM");
            ((TextBox)frm.Controls["textBox2"]).DataBindings.Add("text", tb, "TenDM");
   
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="MaDM"></param>
        public void hienthi(string MaDM) {
            con.Open();
            //SqlCommand cmd = new SqlCommand();
            //cmd.Connection = con.getconn;
            //cmd.CommandText = "ps_laydanhmuc";
            //cmd.CommandType = System.Data.CommandType.StoredProcedure;
            //SqlParameter p1;
            //if (MaDM != null)
            //    p1 = new SqlParameter("@MaDM", MaDM);
            //else
            //    p1 = new SqlParameter("@MaDM", DBNull.Value);
            //cmd.Parameters.Add(p1);
            //SqlDataAdapter adp = new SqlDataAdapter(cmd);
            //DataTable tb = new DataTable();
            //adp.Fill(tb);
            SqlParameter[] pr = new SqlParameter[1];
            if (MaDM != null)
                pr[0] = new SqlParameter("@MaDM", MaDM);
            else
                pr[0] = new SqlParameter("@MaDM", DBNull.Value);
           tb = con.Execute_ps("ps_laydanhmuc", pr);
           refreshdata();
     //       ((DataGridView)frm.Controls["dataGridview1"]).DataSource = tb;
       //     ((TextBox)frm.Controls["textBox1"]).DataBindings.Add("text", tb, "MaDM");
         //   ((TextBox)frm.Controls["textBox2"]).DataBindings.Add("text", tb, "TenDM");
            con.Close();
        }
        public void them(string MaDM, string TenDM)
        {
            con.Open();
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter("@MaDM",MaDM);
            pr[1]=new SqlParameter("@TenDM",TenDM);
            DataTable tb=con.Execute_ps("ps_danhmuc1",pr);
            refreshdata();
           // ((DataGridView)frm.Controls["dataGridView1"]).DataSource = tb;
        }
        public void themmoi()
        {
            string madm,tendm;
            madm = ((TextBox)frm.Controls["TextBox1"]).Text;
            tendm = ((TextBox)frm.Controls["TextBox2"]).Text;
            SqlParameter []pr = {new SqlParameter("@MaDM", madm),new SqlParameter("@TenDM",tendm)};
            con.Open();
             tb = con.Execute_ps("ps_danhmuc1",pr);
             refreshdata();
            //((DataGridView)frm.Controls["dataGridview1"]).DataSource = tb;
 
            con.Close();

        }
        public void sua(string MaDM, string TenDM)
        {
            con.Open();
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter("@MaDM", MaDM);
            pr[1] = new SqlParameter("@TenDM", TenDM);
             tb = con.Execute_ps("ps_suadanhmuc", pr);
             refreshdata();
            //((DataGridView)frm.Controls["dataGridView1"]).DataSource = tb;
        }
        public void sua2()
        {
            string madm, tendm;
            madm = ((TextBox)frm.Controls["TextBox1"]).Text;
            tendm = ((TextBox)frm.Controls["TextBox2"]).Text;
            SqlParameter[] pr = { new SqlParameter("@MaDM", madm), new SqlParameter("@TenDM", tendm) };
            con.Open();
             tb = con.Execute_ps("ps_suadanhmuc", pr);
             refreshdata();
            con.Close();
 
        }
        public void xoa2()
        {
            string madm;
            madm = ((TextBox)frm.Controls["TextBox1"]).Text;
           //tendm = ((TextBox)frm.Controls["TextBox2"]).Text;
           // SqlParameter[] pr = { new SqlParameter("@MaDM", madm), new SqlParameter("@TenDM", tendm) };
            SqlParameter[] pr = { new SqlParameter("@MaDM", madm) };
            con.Open();
             tb = con.Execute_ps("ps_xoadanhmuc", pr);
            //((DataGridView)frm.Controls["dataGridView1"]).DataSource = tb;
             refreshdata();
             con.Close();
        }
        public void xoa(string MaDM)
        {
            con.Open();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter("@MaDM", MaDM);
            //pr[1] = new SqlParameter("@TenDM", TenDM);
             tb = con.Execute_ps("ps_xoadanhmuc", pr);
             refreshdata();
 //           ((DataGridView)frm.Controls["dataGridView1"]).DataSource = tb;
        }
    }
}